package com.mryx.demo.controller;

import com.alibaba.fastjson.JSON;
import com.mryx.demo.util.SDKTestSendTemplateSMS;
import com.mryx.demo.pojo.User;
import com.mryx.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = {"用户模块"})
public class UserController {
    @Resource(name="userService")
    UserService userService;
    @Resource(name="senderCode")
    SDKTestSendTemplateSMS senderCode;
    private String num;
    private String phoneNumber;

    @Resource
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 向用户发送验证码
     * @param phoneNum 手机号
     * @return
     */
    @PostMapping("/senderCode/{phoneNum}")
    @ApiOperation(value = "登录/注册(1)：向用户发送验证码",notes = "向用户发送验证码")
    @ApiImplicitParam(name = "user",value = "user对象",required = true,paramType = "path",dataType = "String")
    public String senderCode(@PathVariable String phoneNum){
        phoneNumber=phoneNum;
        User user=userService.getUserByPhone(phoneNum);
        num=senderCode.SendVerCode(phoneNum);
        System.out.println("向用户发送的验证码为"+num);
        if(null!=user){
            return "登录用户验证码发送成功";
        }
        return "注册用户验证码发送成功，验证成功自动注册";
    }

    /**
     * 登录or注册验证
     * @param code
     * @return
     */
    @PostMapping("/login/{code}")
    @ApiOperation(value = "登录/注册(2)：验证",notes = "登录：验证")
    @ApiImplicitParam(name = "code",value = "验证码",required = true,paramType = "path",dataType = "String")
    public String login(@PathVariable String code){
        System.out.println("生成验证"+num);
        System.out.println("输入验证"+code);
        if(num.equals(code)){
            User user=userService.getUserByPhone(phoneNumber);
            if(null!=user){
               redisTemplate.opsForValue().set("uid", user.getUid().toString());
                return "登录用户验证成功";
            }
            Integer res=userService.register(phoneNumber);
            if (res>0){
                return "用户注册成功";
            }
        }
        return "验证失败";
    }
    /**
     * 修改用户收货地址
     * @param user user对象
     * @return 修改是否成功
     */
    @PostMapping("/updAddress")
    @ApiOperation(value = "用户修改收货地址",notes = "用户修改收获地址")
    @ApiImplicitParam(name = "user",value = "user对象",required = false,dataType = "User")
    public String updAddress(User user){
        Integer res=userService.updAddress(user);
        if (res>0){
            return JSON.toJSONString("0");
        }
        return JSON.toJSONString("1");
    }

    /**
     * 查看用户积分
     * @param uid 用户id
     * @return 用户积分
     */
    @GetMapping("/getIntegral/{uid}")
    @ApiOperation(value = "查看用户积分",notes = "查看用户积分")
    @ApiImplicitParam(name = "uid",value = "用户id",required = true,paramType = "path",dataType = "int")
    public String getIntegral(@PathVariable Integer uid){ return JSON.toJSONString(userService.getIntegral(uid)); }

    /**
     * 用户修改支付密码
     * @param user 用户对象
     * @return 是否修改成功
     */
    @PostMapping("/updPayPwd")
    @ApiOperation(value = "修改支付密码",notes = "用户修改或设置支付密码")
    @ApiImplicitParam(name = "user",value = "用户对象",required = false,dataType = "User")
    public String updPayPwd(User user){
        Integer res=userService.updPayPwd(user);
        if (res>0){
            return JSON.toJSONString("0");
        }
        return JSON.toJSONString("1");
    }

    /**
     * 修改手机号码
     * @param user 用户对象
     * @return 是否修改成功
     */
    @PostMapping("/updPhone")
    @ApiOperation(value = "修改手机号码",notes = "用户修改手机号码")
    @ApiImplicitParam(name = "user",value = "用户对象",required = false,dataType = "User")
    public String updPhone(User user){
        Integer res=userService.updPhone(user);
        if (res>0){
            return JSON.toJSONString("0");
        }
        return JSON.toJSONString("1");
    }

    /**
     * 查看用户余额
     * @param uid 用户id
     * @return 用户余额
     */
    @GetMapping("/getBalance/{uid}")
    @ApiOperation(value = "查看用户余额",notes = "查看用户余额")
    @ApiImplicitParam(name = "uid",value = "用户id",required = false,paramType = "path",dataType = "int")
    public  String getBalance(@PathVariable Integer uid){return JSON.toJSONString(userService.getBalance(uid)); }

    /**
     * 管理员修改用户余额
     * @param user 用户对象
     * @return 是否修改成功
     */
    @PostMapping("/updBalance")
    @ApiOperation(value = "管理员修改用户余额",notes = "管理员修改用户余额")
    @ApiImplicitParam(name = "user",value = "用户对象",required = false,dataType = "User")
    public  String updBalance(User user){
        Integer res=userService.updBalance(user);
        if (res>0){
            return JSON.toJSONString("0");
        }
        return JSON.toJSONString("1");
    }

    /**
     * 管理员修改用户状态
     * @param uid 用户编号
     * @return 是否修改成功
     */
    @PostMapping("/updState/{uid}")
    @ApiOperation(value = "管理员修改用户状态",notes = "管理员修改用户状态")
    @ApiImplicitParam(name = "uid",value = "用户id",required = false,dataType = "int")
    @CrossOrigin
    public String updState(@PathVariable Integer uid){
        User user=userService.getInfo(uid);
        Integer res=userService.updState(user);
        if (res>0){
            return JSON.toJSONString("0");
        }
        return JSON.toJSONString("1");
    }

    /**
     * 获取用户详情
     * @param uid 用户id
     * @return user对象
     */
    @GetMapping("/getInfo/{uid}")
    @ApiOperation(value = "获取用户详情",notes = "获取用户详情")
    @ApiImplicitParam(name = "uid",value = "用户id",required = true,paramType = "path",dataType = "int")
    public String getInfo(@PathVariable Integer uid){return JSON.toJSONString(userService.getInfo(uid));}

    /**
     * 管理员查看用户列表
     * @return list<User>
     */
    @GetMapping("/getAllUser")
    public String getAllUser(Integer currentPage){return JSON.toJSONString(userService.getAllUser(currentPage,5));}
}
